package com.qjc.demo.train.gragh;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Gragh1
 * @Description: 用邻接矩阵的方式实现图
 * @Author: qjc
 * @Date: 2022/5/2 9:07 PM
 */
public class Gragh1 {
    private List vertextList;// 存储点的链表
    private int[][] edges;// 邻接矩阵，用来存储边
    private int numOfEdges;// 边的数量

    public Gragh1(int n) {
        // 初始化矩阵，一维数组和边的数目
        vertextList = new ArrayList();
        edges = new int[n][n];
        numOfEdges = 0;
    }

    // 得到点的数目
    public int getNumOfVertex() {
        return vertextList.size();
    }

    // 得到边的数目
    public int getNumOfEdges() {
        return numOfEdges;
    }

    // 返回结点i的值
    public Object getValueByIndex(int i) {
        return vertextList.get(i);
    }

    // 返回结点V1-V2的权重
    public int getWeight(int V1, int V2) {
        return edges[V1][V2];
    }

    // 插入结点
    public void insertVertex(Object vertex) {
        vertextList.add(vertex);
    }

    // 插入边
    public void insertEdge(int i, int j, int w) {
        edges[i][j] = w;
        numOfEdges++;
    }

    public static void main(String[] args) {
        String[] vertexes = {"V1", "v2", "V3", "V4"};
        Gragh1 gragh1 = new Gragh1(vertexes.length);
        for (String vertex : vertexes) {
            gragh1.insertVertex(vertex);
        }
        gragh1.insertEdge(0, 1, 2);
        gragh1.insertEdge(1, 2, 4);
        gragh1.insertEdge(2, 3, 2);
        gragh1.insertEdge(0, 3, 4);

        System.out.println(gragh1.getNumOfVertex());// 获取结点个数
        System.out.println(gragh1.getNumOfEdges());// 获取边的个数
        System.out.println(gragh1.getWeight(0, 1));// 获取边的权重
    }


}
